﻿Imports System.Windows.Forms
Imports System.Net.Mail
Imports System.Collections.ObjectModel
''' <summary>
''' This form will be called when an unhandled exception is thrown
''' Once Loaded, it does some research on the problem and ask permission
''' from the user to send an email to development group that contains
''' all of the information the form has collected.
''' </summary>
''' <remarks></remarks>
Public Class DlgUnhandledError
    Public ErrorMessage As String
    Public Options As String
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        On Error Resume Next
        FrmMain.Tray.Visible = False
        For Each frm As Form In Application.OpenForms
            frm.Hide()
        Next

        MsgBox("زۆر سوپاسی هاریکاریت دەکەین.")
        Dim Smtp_Server As New SmtpClient
        Dim e_mail As New MailMessage()
        Smtp_Server.UseDefaultCredentials = False
        Smtp_Server.Credentials = New Net.NetworkCredential(MsgSender, "Password")
        Smtp_Server.Port = 587
        Smtp_Server.EnableSsl = True
        Smtp_Server.Host = "smtp.gmail.com"

        e_mail = New MailMessage()
        e_mail.From = New MailAddress(MsgSender)
        e_mail.To.Add(Email)
        e_mail.Subject = "تێکشکانی بانگبێژ ٢.٠"
        e_mail.IsBodyHtml = False
        e_mail.Body = MessageText
        Smtp_Server.Send(e_mail)

        Me.Close()
    End Sub

    Private Sub Me_FormClosed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosed
        Application.Exit()
        End
    End Sub
    Dim MsgSender As String = "Bangbezh@gmail.com"
    Dim MessageText As String
    Private Sub DlgUnhandledError_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        On Error Resume Next
        Dim Options As String
        Options = My.Computer.FileSystem.ReadAllText(GetStartupPath() + "\Data\Options.bbj")

        My.Settings.Crashes += 1
        My.Settings.Save()
        MessageText = Nothing
        MessageText += "هۆکاری تێکشکان: " + Environment.NewLine + ErrorMessage + Environment.NewLine
        MessageText += "ژمارەی تێکشکان: " + My.Settings.Crashes.ToString + Environment.NewLine
        MessageText += "پارێزگای دیاریکراو: "
        If City < 5 Then
            MessageText += Cities(City) + Environment.NewLine
        Else
            MessageText += City + Environment.NewLine
        End If
        MessageText += "بەروار: " + Now.Date.ToString("yyyy/MM/dd") + Environment.NewLine
        MessageText += "کات: " + Now.ToLongTimeString + Environment.NewLine
        MessageText += "سیستەمی کارپێکردن: " + My.Computer.Info.OSFullName + Environment.NewLine
        MessageText += "وەشانی سیستەمی کارپێکردن: " + My.Computer.Info.OSVersion + Environment.NewLine
        MessageText += "شوێنی دامەزراندنی بانگبێژ: " + Environment.NewLine + GetStartupPath() + Environment.NewLine
        MessageText += "گوورزەی خزمەتگوزاری: "
        If Environment.OSVersion.ServicePack Is Nothing Then
            MessageText += Environment.OSVersion.ServicePack
        Else
            MessageText += "نییە"
        End If
        MessageText += Environment.NewLine

        MessageText += "تەلارسازی: "
        If IntPtr.Size = 8 Then
            MessageText += "X64"
        ElseIf IntPtr.Size = 4 Then
            MessageText += "X86"
        Else
            MessageText += "نەزاندراو"
        End If
        MessageText += Environment.NewLine
        MessageText += "-------------------------------------------------" + Environment.NewLine
        MessageText += "ڕێکخستنەکان: "
        If Options <> Nothing Then
            MessageText += Environment.NewLine + Options
        Else
            MessageText += "نەزاندراو"
        End If
        MessageText += Environment.NewLine
        MessageText += "-------------------------------------------------" + Environment.NewLine
        MessageText += "فایل و فۆڵدەرەکان: " + Environment.NewLine
        For Each Item In getfiles()
            MessageText += Item + Environment.NewLine
        Next

        TxtMessage.Text = MessageText
        Exit Sub
errr:
        MsgBox(Err.Description)
    End Sub

    Private Sub Cancel_Button_Click(sender As Object, e As EventArgs) Handles Cancel_Button.Click
        Me.Close()
    End Sub

    Private Sub Label2_Clicked(sender As Object, e As EventArgs) Handles Label2.Click
        Dim msg As String
        msg = "گرووپی پەرەپێدەرانی بانگبێژ ئەو زانیارییانە تەنیا بۆ مەبەستی " _
            + "باشترکردنی بانگبێژ بەکاردەهێنن و بەهیچ شێوەیەک زانیاری تاکەکەسیت " _
            + "کۆناکرێتەوە وە بێجگە لەو نووسینەی کە نیشاندراوە، بانگبێژ هیچ شتێکی دیکە نانێرێت"
        MsgBox(msg)
    End Sub

    Private Sub Label2_MouseEnter(sender As Object, e As EventArgs) Handles Label2.MouseEnter
        Label2.Font = New Font("Segoe UI", 10, FontStyle.Underline)
    End Sub

    Private Sub Label2_MouseLeave(sender As Object, e As EventArgs) Handles Label2.MouseLeave
        Label2.Font = New Font("Segoe UI", 10, FontStyle.Regular)
    End Sub

    Function getfiles() As List(Of String)
        Dim files As ReadOnlyCollection(Of String)
        Dim dirs As ReadOnlyCollection(Of String)
        Dim both As New List(Of String)
        files = My.Computer.FileSystem.GetFiles(Application.StartupPath, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
        dirs = My.Computer.FileSystem.GetDirectories(Application.StartupPath, FileIO.SearchOption.SearchAllSubDirectories)

        both.Add("####### فۆڵدەرەکان ####### ")
        For Each Item In dirs
            both.Add(Item.Replace(GetStartupPath, Nothing))
        Next

        both.Add("######## فایلەکان ######## ")
        For Each Item In files
            both.Add(Item.Replace(GetStartupPath, Nothing))
        Next
        Return both
    End Function
  
End Class
